package thread.baozi;

public class Product extends Thread {
    private BaoZi baoZi;

    public Product(BaoZi baoZi) {
        this.baoZi = baoZi;
    }

    @Override
    public void run() {
        int count = 0;
        while (true) {
            synchronized (baoZi) {
                if (baoZi.isFlag()) {
                    try {
                        baoZi.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                if (count % 2 == 0) {
                    baoZi.setPi("薄皮");
                    baoZi.setXian("羊肉馅");
                } else {
                    baoZi.setPi("厚皮");
                    baoZi.setXian("牛肉馅");
                }
                count++;
                System.out.println("包子铺正在生产" + baoZi.getPi() + baoZi.getXian() + "的包子");

                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                baoZi.setFlag(true);
                baoZi.notify();
                System.out.println("包子铺已经生产" + baoZi.getPi() + baoZi.getXian() + "的包子,请享用");
            }
        }
    }
}
